"""
家 | 业务层
"""

from biz.base_biz import (
    CreatorBase,
    DeleterBase,
    GetterBase,
    UpdatorBase,
)
from biz.family_biz.base import FamilyValidatorBase
from components.global_param import get_user_id
from mapping import FamilyHomeMapping
from models.po import FamilyHomePO
from models.request import (
    FamilyHomeCreateRequest,
    FamilyHomeDetailRequest,
    FamilyHomeListRequest,
    FamilyHomeUpdateRequest,
)
from models.vo import FamilyHomeDetailVO
from my_typing import *
from service import (
    FamilyHomeService,
    FamilyRoomService,
)


class Model:
    """ 模型信息 """

    ID_FIELD: str = "home_id"
    NAME: str = "家"
    PO = FamilyHomePO
    SERVICE = FamilyHomeService


class FamilyHomeValidator(FamilyValidatorBase):
    """ 家 | 校验器 """

    model_id_field = Model.ID_FIELD
    model_name = Model.NAME
    model_po = Model.PO
    model_service = Model.SERVICE
    cited_model_service = FamilyRoomService

    # -------------------- validate_xxx --------------------

    @classmethod
    def validate_region_code(
        cls,
        region_code: StrOrNone,
    ) -> None:
        """ 校验 | 行政区划编号是否合法 """

        from biz.region_biz import RegionValidator

        RegionValidator.validate_code_valid(region_code)

    # -------------------- validate_crud --------------------

    @classmethod
    def validate_create(cls, request: FamilyHomeCreateRequest) -> None:
        """
        校验 | 创建

        1. 名称是否存在
        2. 别名是否合法
        3. 行政区划编号是否合法
        """

        cls.validate_union(
            {
                "alias": request.alias,
                "name": request.name,
                "region_code": request.region_code,
                "user_id": get_user_id(),
            },
            cls.validate_name_exist,
            cls.validate_alias,
            cls.validate_region_code,
        )

    @classmethod
    def validate_update(cls, request: FamilyHomeUpdateRequest) -> None:
        """
        校验 | 更新

        1. 家是否存在
        2. 同 `校验 | 创建`
        """

        cls.validate_union(
            {
                "id": request.home_id,
                "home_id": request.home_id,
                "alias": request.alias,
                "name": request.name,
                "region_code": request.region_code,
                "user_id": get_user_id(),
            },
            cls.validate_exist,
            cls.validate_name_exist,
            cls.validate_alias,
            cls.validate_region_code,
        )

    @classmethod
    def validate_delete(cls, request: FamilyHomeDetailRequest) -> None:
        """
        校验 | 删除

        1. 家是否存在
        2. 家是否是当前用户的
        3. 家是否被引用
        """

        cls.validate_union(
            {
                "id": request.home_id,
                "home_id": request.home_id,
                "user_id": get_user_id(),
            },
            cls.validate_exist,
            cls.validate_mine,
            cls.validate_cited,
        )


class FamilyHomeGetter(GetterBase):
    """ 家 | 获取器 """

    model_mapping = FamilyHomeMapping
    model_validator = FamilyHomeValidator

    @classmethod
    def get_list_query(cls, request: FamilyHomeListRequest) -> dict:
        condition = cls.Condition({"user_id": get_user_id()})

        condition.add_eq("region_code", value=request.region_code)
        condition.add_regex("alias", value=request.alias)
        condition.add_regex("description", value=request.description)
        condition.add_regex("name", value=request.name)

        return condition.get_query()


class FamilyHomeCreator(CreatorBase):
    """ 家 | 创建器 """

    @staticmethod
    def _create_data(request: FamilyHomeCreateRequest) -> FamilyHomePO:
        """ 创建数据 """

        return FamilyHomePO(
            **request.model_dump(),
            user_id=get_user_id(),
        )

    @classmethod
    def create(cls, request: FamilyHomeCreateRequest) -> FamilyHomeDetailVO:
        """ 家 | 创建 """

        FamilyHomeValidator.validate_create(request)
        home_po: FamilyHomePO = cls._create_data(request)
        cls.version_log(home_po)
        vo: FamilyHomeDetailVO = FamilyHomeMapping.po_2_detail_vo(home_po)

        return vo


class FamilyHomeDeleter(DeleterBase):
    """ 家 | 删除器 """

    @staticmethod
    def _delete_data(request: FamilyHomeDetailRequest) -> FamilyHomePO:
        """ 删除数据 """

        old_home_po: FamilyHomePO = FamilyHomeService.get_one_by_id(request.home_id)
        new_home_po: FamilyHomePO = old_home_po.model_copy(deep=True)
        FamilyHomeService.delete_one(new_home_po)

        return old_home_po

    @classmethod
    def delete(cls, request: FamilyHomeDetailRequest) -> None:
        """ 删除家 """

        FamilyHomeValidator.validate_delete(request)
        home: FamilyHomePO = cls._delete_data(request)
        cls.version_log(home)


class FamilyHomeUpdator(UpdatorBase):
    """ 家 | 更新器 """

    @staticmethod
    def _update_data(request: FamilyHomeUpdateRequest) -> tuple[FamilyHomePO, FamilyHomePO]:
        """ 更新数据 """

        old_home_po: FamilyHomePO = FamilyHomeService.get_one_by_id(request.home_id)
        new_home_po: FamilyHomePO = old_home_po.model_copy()
        new_home_po.update_from_request(request)
        FamilyHomeService.update_one(new_home_po)

        return old_home_po, new_home_po

    @classmethod
    def update(cls, request: FamilyHomeUpdateRequest) -> FamilyHomeDetailVO:
        """ 更新家 """

        FamilyHomeValidator.validate_update(request)
        old_home_po, new_home_po = cls._update_data(request)
        cls.version_log(old_home_po, new_home_po)
        vo: FamilyHomeDetailVO = FamilyHomeMapping.po_2_detail_vo(new_home_po)

        return vo
